package com.anysoft.user.bankcard;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.anysoft.HostConstant;
import com.anysoft.MentorConst;
import com.anysoft.R;
import com.anysoft.bean.GetBankCardListBean;
import com.anysoft.utils.HttpUtils;
import com.anysoft.view.dialog.IcbcDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName：gy_fund-android
 * @ClassDescription：
 * @CreateUser： yanbin
 * @CreateTime：2017-02-15 14:08
 * @ModifiedUser：
 * @ModifiedTime：
 * @ModifiedRemark：
 */

public class BankCardFragment extends Fragment implements View.OnClickListener{
    final static private String TAG = "BankCardFragment";
    private TextView tv_bank_card_code;
    private Button btn_unbind;
    GetBankCardListBean.BankCardInfo bankCardInfo;
    View root;
    public static BankCardFragment newInstance(int index,GetBankCardListBean.BankCardInfo bankCardInfo) {
        BankCardFragment f = new BankCardFragment();
        Bundle b = new Bundle();
        b.putInt("index",index);
        b.putSerializable("BankCardInfo", bankCardInfo);
        f.setArguments(b);
        return f;
    }


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        if (null == root) {
            root = inflater.inflate(R.layout.bank_cark_fragment, container, false);
            initView();
        }
        Bundle args = getArguments();
         if(args != null) {
             bankCardInfo = (GetBankCardListBean.BankCardInfo) args.getSerializable("BankCardInfo");
             bindData(args.getInt("index"),bankCardInfo);
         }
        return root;

    }
    private void initView(){
        tv_bank_card_code = (TextView) root.findViewById(R.id.tv_bank_card_code);
        btn_unbind = (Button) root.findViewById(R.id.btn_unbind);
        btn_unbind.setOnClickListener(this);
    }
    private void bindData(int index, GetBankCardListBean.BankCardInfo bankCardInfo){
        this.index = index;
        this.bankCardInfo = bankCardInfo;
        String accountNo = bankCardInfo.getBankAccountNo();
        String accountNo_tmp="";
        if(accountNo!=null){
            int count = accountNo.length()/4;
            if(accountNo.length()%4==0){
                count--;
            }
            if(count<=0){
                accountNo_tmp = accountNo;
            }
            for(int i=0;i<count;i++){
                accountNo_tmp += accountNo.substring(i*4,i*4+4)+" ";
                if(i==count-1){
                    accountNo_tmp += accountNo.substring(i*4+4);
                }
            }
            if(accountNo.length()%4>0){
                count--;
            }
        }
        tv_bank_card_code.setText(accountNo_tmp+"");
    }

    private int index;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_unbind:
//                Toast.makeText(getContext(),"解绑卡号"+index,Toast.LENGTH_SHORT).show();
                String accountNo = bankCardInfo.getBankAccountNo();
                if(accountNo!=null && accountNo.length()>4){
                    accountNo = accountNo.substring(accountNo.length()-4);
                }
                IcbcDialog dialog = new IcbcDialog(getContext());
                dialog.setColor("#ffffff");
                dialog.setTitleTop(5);
                dialog.setContentLeft(20);
                dialog.setContentRight(20);
                dialog.setContentTextGravity(Gravity.CENTER_HORIZONTAL);
                dialog.isContentTextHtml(true);
                dialog.setCancelable(false);
                dialog.setAnimationEnable(true);
                dialog.setTitle("确认解绑");
                dialog.setContentText(String.format("确认解绑 %s 银行卡尾号<font color=#ff0000>%s</font>",bankCardInfo.getBankName(),accountNo));
                dialog.setPositiveListener("确定", new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                        inputPwd();
                    }
                });
                dialog.setNegativeListener("跳过",new IcbcDialog.OnNegativeListener(){
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                    }

                }).show();
                break;
        }
    }

    private void inputPwd(){
        IcbcDialog dialog = new IcbcDialog(getContext());
        dialog.setColor("#ffffff");
        dialog.setEditTextColor("#000000");
        dialog.setCancelable(false);
        dialog.setAnimationEnable(true);
        dialog.setTitle("输入密码");
        dialog.setEditTextType(true);
        dialog.setNegativeListener("跳过", new IcbcDialog.OnNegativeListener() {
            @Override
            public void onClick(IcbcDialog dialog) {
                dialog.dismiss();
            }
        });
        dialog.setPositiveListener("确定", new IcbcDialog.OnPositiveListener() {
            @Override
            public void onClick(IcbcDialog dialog) {
                String input = dialog.getEditTextContent();
                if (TextUtils.isEmpty(input)) {
                    Toast.makeText(getContext(), "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(getContext(), "进行密码校验", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        }).show();

//        DialogUtils utils = new DialogUtils();
//        utils.edittextAlert(getContext(), "输入密码", "确定", "跳过", "请输入密码");
//        utils.setOnEdittextDialogSbtnListener(new DialogUtils.OnEdittextDialogSbtnListener() {
//            @Override
//            public void OnEdittextDialogSbtnClick(String inputContent) {
//                Toast.makeText(getContext(),inputContent,Toast.LENGTH_SHORT).show();
//            }
//        });
    }
    private void unBindBankCard(){
        Map<String, String> params = new HashMap<>();
        params.put("dsCustomerNo", "");//replace 基金公司客户号
        params.put("tradeAccountNo", "");//基金公司用户交易账号

        Logger.t("-----CLOSE_ACCOUNT-----").d(HttpUtils.createRequestUrl(getContext(), HostConstant.CLOSE_ACCOUNT, params));
        OkGo.<String>post(HostConstant.CLOSE_ACCOUNT)
                .tag(this)
                .params(HttpUtils.getCommonParams(getContext(), params))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                    }


                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast(MentorConst.INTERNET_ERROR);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }
    private void showToast(String msg){
        Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
    }
}
